package com.example.demo.service.Impl;

import com.example.demo.common.utills.AliyunSmsUtils;
import com.example.demo.common.utills.CommonResult;
import com.example.demo.common.utills.PasswordEncryptUtils;
import com.example.demo.entity.Code;
import com.example.demo.entity.User;
import com.example.demo.mapper.LoginMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    LoginMapper loginMapper;


    @Override
    public CommonResult selectAllUser(int pageNum,int pageSize,String username,int uid) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectAllUser(username,uid);
        PageInfo<List<User>> pageInfo=new PageInfo(users);
        return new CommonResult(200,"查询成功",pageInfo);
    }

    @Override
    public CommonResult addUser(User user) {
        String account = user.getAccount();
        String password = user.getPassword();
        String ePsd= PasswordEncryptUtils.MyPasswordEncryptUtil(account,password);
        user.setPassword(ePsd);
        int i = userMapper.addUser(user);
        if(i!=0){
            return new CommonResult(200,"新增成功");
        }
        return new CommonResult(500,"添加失败");
    }

    @Override
    public CommonResult deleteUser(int uid) {
        int i = userMapper.deleteUser(uid);
        if(i!=0){
            return new CommonResult(200,"删除成功");
        }
        return new CommonResult(500,"删除失败");
    }

    @Override
    public CommonResult resetPsd(String psd, String account) {
        String newPsd=PasswordEncryptUtils.MyPasswordEncryptUtil(account,psd);
        int i = userMapper.resetPsd(newPsd, account);
        if(i!=0){
            return new CommonResult(200,"重置密码成功");
        }
        return new CommonResult(500,"重置密码失败");
    }

    @Override
    public CommonResult sendMes(String account,String phone) throws Exception {
        List<User> user1 = loginMapper.selectUP(account);
        if(user1.isEmpty()){
            return new CommonResult(401,"账户未存在");
        }
        List<User> user = loginMapper.selectUP(phone);
        if(user.isEmpty()){
            return new CommonResult(403,"该电话号码未绑定");
        }
        try {
            String s=AliyunSmsUtils.SendSms(phone,userMapper);
            if("fail".equals(s))
                return new CommonResult(500,"验证码发送失败");
            return new  CommonResult(200,"验证码发送成功");
        }catch (NullPointerException e){
            return new CommonResult(500,"发送失败");
        }
    }

    @Override
    public CommonResult forgetPsd(String account,String phone,String code) {
        Code codeR = userMapper.selectCode(code);
        if(codeR!=null){
            User user= userMapper.forgetPsd(account,phone);
            if(user!=null){
                userMapper.deleteCode(code);
                return new CommonResult(200,"信息确认成功");
            }
            return new CommonResult(500,"电话号码未绑定该账号");
        }
        return new CommonResult(500,"验证码错误");
    }
}
